package com.ethink.warehouse.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ethink.framework.common.db.PageUtils;
import com.ethink.warehouse.api.request.MatePurchaseCodeQueryRequest;
import com.ethink.warehouse.api.request.MatePurchaseCodeUpdateRequest;
import com.ethink.warehouse.api.response.MatePurchaseCodeGetDetailResponse;
import com.ethink.warehouse.api.response.MatePurchaseCodeQueryResponse;
import com.ethink.warehouse.common.entity.MateProduceCode;
import com.ethink.warehouse.common.entity.MatePurchaseCode;
import com.ethink.warehouse.common.entity.MatePurchaseCodeChangeLog;
import com.ethink.warehouse.common.mapper.MatePurchaseCodeChangeLogMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ethink.warehouse.common.mapper.MatePurchaseCodeMapper;
import com.ethink.warehouse.converter.MatePurchaseCodeConverter;
import com.ethink.warehouse.service.MatePurchaseCodeService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 库存采购批次表 Service接口实现
 *
 * @author default
 * @date 2022-12-16
 */
@Slf4j
@Service
public class MatePurchaseCodeServiceImpl implements MatePurchaseCodeService {

    @Autowired
    private MatePurchaseCodeMapper matePurchaseCodeMapper;

    @Autowired
    private MatePurchaseCodeChangeLogMapper matePurchaseCodeChangeLogMapper;

    @Autowired
    private MatePurchaseCodeConverter matePurchaseCodeConverter;

    @Override
    public Page<MatePurchaseCodeQueryResponse> queryMatePurchaseCode(MatePurchaseCodeQueryRequest request) {
        return matePurchaseCodeMapper.queryMatePurchaseCode(PageUtils.buildPage(request), request);
    }

    @Override
    public MatePurchaseCodeGetDetailResponse getMatePurchaseCodeById(Long matePurchaseCodeId) {
        return matePurchaseCodeMapper.getMatePurchaseCodeById(matePurchaseCodeId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateMatePurchaseCode(MatePurchaseCodeUpdateRequest request) {
        MatePurchaseCode matePurchaseCode = matePurchaseCodeMapper.selectById(request.getId());
        MatePurchaseCodeChangeLog matePurchaseCodeChangeLog = new MatePurchaseCodeChangeLog();
        matePurchaseCodeChangeLog.setTargetId(matePurchaseCode.getId());
        matePurchaseCodeChangeLog.setMateSku(matePurchaseCode.getMateSku());
        matePurchaseCodeChangeLog.setPurchaseCode(matePurchaseCode.getPurchaseCode());
        matePurchaseCodeChangeLog.setPurchaseOrderNo(matePurchaseCode.getPurchaseOrderNo());
        matePurchaseCodeChangeLog.setPurchaseDetailOrderNo(matePurchaseCode.getPurchaseDetailOrderNo());
        matePurchaseCodeChangeLog.setCostPrice(matePurchaseCode.getCostPrice());
        matePurchaseCodeChangeLog.setProduceCode(matePurchaseCode.getProduceCode());
        matePurchaseCodeChangeLog.setCountryOfOrigin(matePurchaseCode.getCountryOfOrigin());
        matePurchaseCodeChangeLog.setProductDate(matePurchaseCode.getProductDate());
        matePurchaseCodeChangeLog.setExpireDate(matePurchaseCode.getExpireDate());
        matePurchaseCodeChangeLog.setType(matePurchaseCode.getType());
        matePurchaseCodeChangeLogMapper.insert(matePurchaseCodeChangeLog);

        LambdaUpdateWrapper<MatePurchaseCode> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(MatePurchaseCode::getId, request.getId());
        // FIXME 按实际业务需求set
//        updateWrapper.set(MatePurchaseCode::getCostPrice, request.getCostPrice());
        updateWrapper.set(MatePurchaseCode::getProduceCode, request.getProduceCode());
        updateWrapper.set(MatePurchaseCode::getCountryOfOrigin, request.getCountryOfOrigin());
        updateWrapper.set(MatePurchaseCode::getProductDate, request.getProductDate());
        updateWrapper.set(MatePurchaseCode::getExpireDate, request.getExpireDate());
        matePurchaseCodeMapper.update(null, updateWrapper);
    }

}
